<?php
/**
 * @link http://www.anabas.tk/awc/wiki
 *
 * @package Aba Sulution
 * @subpackage Aba_Core
 * @since Aba Core 1.0.1 beta
 */

defined( 'ABSPATH' ) or die(/*Nothing...*/);

abstract class AWC_Widget extends WP_Widget implements IAWC_API {
    //public  $id = '';
    
    //public  $id_base = 'widget_';
    
    public  $form_fields = array();
    
    public  $configs = array();
    
    private $configs_api;
    
    public $active = true;
    
    public $args        = array(
        'bcontainer'    => '',
        'econtainer'    => '',
        'bfield'        => '<p>',
        'efield'        => '</p>',
        'blabel'        => '',
        'elabel'        => '',
        'bop'           => '',
        'eop'           => '',
        'hide_title'    => true,
    );
    
    function output( $args, $configs ){
        die('@override output');
    }
    
    function init_form(){ 
        die('@override form fields');
    }
    
    function is_active(){
        return $this->active;
    }
    
    function trigger(){
        if ( !$this->is_active() )
            return false;
        add_action( 'widgets_init', array( $this, '_widgets_init_reg' ) );
        return true;
    }
    
    function __construct( $id, $name, $form_fields = array() ){
        parent::__construct( $id, $name );
        $this->configs_api = new AWC_Settings();
        $this->configs_api->id_base = 'widget';
        $this->configs_api->id = $id;
        $this->configs_api->define = array(
            'name' => array( $this, 'get_field_name' ),
            'id'    => array( $this, 'get_field_id' )
        );
        $this->configs_api->args = $this->args;
        $this->form_fields = $this->configs_api->form_fields = $form_fields;
        
        $this->init();
    }
    
    protected function init(){
        $this->init_form();
        $this->configs_api->form_fields = $this->form_fields;
        $this->configs_api->init_settings();
        
        if ( empty( $this->name ) )
            $this->method_title =   __( 'AWC Widget', AWC_LANG );             
    }
    
    function _widgets_init_reg(){
        register_widget( get_class( $this ) );
    }
    
    function widget($args, $intance){
        $this->configs_api->settings = $this->configs = wp_parse_args(
            $intance,
            $this->configs_api->settings
        );
        $this->output( $args, $intance );
    }
    
    function form( $intance ){
        $this->configs_api->settings = wp_parse_args(
            $intance,
            $this->configs_api->settings
        );
        $this->config();
    }
    
    function get_option( $key ){
        return $this->configs_api->get_option( $key );
    }
    
    function config(){
        $this->configs_api->admin_options();
    }
    
    function update( $new, $old ){
        return wp_parse_args( $new, $this->configs_api->settings );
    }                
}
